package com.zsrt.oa.dept.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.zsrt.oa.dept.dao.DeptDao;
import com.zsrt.oa.dept.dao.impl.DeptDaoImpl;
import com.zsrt.oa.dept.entity.DeptEntity;
import com.zsrt.oa.dept.service.DeptService;

/**
 * 部门的服务层的实现类
 * @author 罗智壕-13424431416
 *
 * 2017年6月3日
 */
public class DeptServiceImpl implements DeptService{
	
	/**
	 * 初始化部门实现类
	 */
	private DeptDao dao = new DeptDaoImpl();
	
	/**
	 * 部门的增加
	 * @param deptEntity 需要增加的对象
	 * @param map 存放结果和数据
	 */
	@Override
	public void add(DeptEntity deptEntity,Map<String,Object> map){
		int result = 0;
		try {
			result = dao.add(deptEntity);
			map.put("result", "success");
			map.put("message", "增加成功");
			map.put("data", result);
		} catch (SQLException e) {
			map.put("result", "error");
			map.put("message", "增加失败");
			map.put("data", result);
			e.printStackTrace();
		}
	}
	
	/**
	 * 部门删除
	 * @param deptEntity
	 */
	@Override
	public void delete(DeptEntity deptEntity,Map<String,Object> map) {
		try {
			dao.delete(deptEntity);
			map.put("result", "success");
			map.put("message", "删除成功");
			map.put("data", 1);
		} catch (SQLException e) {
			map.put("result", "error");
			map.put("message", "删除失败");
			map.put("data", 0);
			e.printStackTrace();
		}
	}
	
	/**
	 * 查询全部部门
	 * @param pageSize 从第几页开始
	 * @param curPage 到第几页结束
	 * @param map 返回结果
	 * @throws SQLException 
	 */
	@Override
	public void query(String pageSize,String curPage,Map<String,Object> map){
		List<DeptEntity> list = new ArrayList<DeptEntity>();
		
		if(null == pageSize || "".equals(pageSize)){
			pageSize = "1";
		}
		int ps = Integer.parseInt(pageSize);
		
		if(null == curPage || "".equals(curPage)){
			curPage = "3";
		}
		int cp = Integer.parseInt(curPage);
		
		try {
			list = dao.query(ps,cp,map);
			map.put("result", "success");
			map.put("message", "查询成功");
			map.put("data", list);
		} catch (SQLException e) {
			map.put("result", "error");
			map.put("message", "查询失败");
			map.put("data", list);
			e.printStackTrace();
		}
	}
	
	/**
	 * 修改填充
	 * @param entity
	 * @param map
	 */
	@Override
	public void updateQuery(DeptEntity entity, Map<String, Object> map) {
		List<DeptEntity> list = new ArrayList<DeptEntity>();
		try {
			list = dao.updateQuery(entity);
			map.put("result", "success");
			map.put("message", "查询成功");
			map.put("data", list);
		} catch (SQLException e) {
			map.put("result", "error");
			map.put("message", "查询失败");
			map.put("data", list);
			e.printStackTrace();
		}
	}
	
	/**
	 * 修改
	 * @param entity
	 * @param map
	 */
	@Override
	public void update(DeptEntity entity, Map<String, Object> map) {
		int counter = 0;
		try {
			counter = dao.update(entity);
			map.put("result", "success");
			map.put("message", "修改成功");
			map.put("data", counter);
		} catch (SQLException e) {
			map.put("result", "error");
			map.put("message", "修改失败");
			map.put("data", counter);
			e.printStackTrace();
		}
	}

}
